<?

//////////// THE "RSS OUT" CLASS ////////////
class RSSfeed {
	function RSSfeed( $vars=0 ){
		global $DB;
		if($vars) $this->_addvars($vars);
	}
	
	function _addvars($vars) { if($vars) foreach($vars as $k=>$v) if($k!=$this) $this->$k=$v; }

}


//////////// THE "RSS IN" CLASS ////////////
class RSSbin {
	function RSSbin( $vars=0 ){
		global $DB;
		if($vars) $this->_addvars($vars);
		if(!$this->what) $this->what = array("ID");
		if(!$this->loop) $this->loop = 30;
	}
	
	function get($am = 1, $what = 0, $feeds = 0) {
		if($this->am) $am = $this->am;
		if($feeds) $this->feeds = is_array($feeds)? $feeds : array($feeds);
		if($what) $this->what = $what;
		
		$this->_checkloop($this->feeds, $this->loop);
		return $this->push_direct($am);
	}
	
	function push_direct($am = 1) {
		global $DB;
		return $DB->parse( implode(",", $this->what), "mod_rss", "feed in(".implode(",", $this->feeds).") and active order by mod_rss.date desc limit $am", true);
	}
	
	function _addvars($vars) { if($vars) foreach($vars as $k=>$v) if($k!=$this) $this->$k=$v; }
	
	function _checkloop( $feeds, $loop = 30 ) {
		global $DB;
		if( $DB->short("1","mod_object","ID in(".implode(",", $feeds).") and TIMESTAMPDIFF(MINUTE, llog, now()) > $loop") ) {
			$updates = $DB->parse("ID, meta, llog","mod_object","ID in(".implode(",", $feeds).") and active and TIMESTAMPDIFF(MINUTE, llog, now()) > $loop", true);
			foreach($updates as $upd) {
				$this->_inject($upd['ID'], $upd);
			}
		}
	}
	
	function _inject($id, $feed=0 ) {
		global $DB;
		if(!$feed) $feed = $DB->parse("ID, meta, llog","mod_object","ID=$id");
		$meta = json_decode($feed['meta']);
		
		if($meta->url) $xmlraw = $this->_content_url($meta->url);
		else if($meta->cjson) $jsonraw = $this->_content_curl($meta->cjson);

		if($xmlraw &&  $xml = new SimpleXMLElement($xmlraw)) {
			if($xml->link[0]->attributes()->type == "application/atom+xml") $this->_pull_atom($xml, $feed);
		
		} else if($jsonraw && $json = json_decode($jsonraw)) {
			//curl http://twitter.com/statuses/user_timeline.json?screen_name=nicklambrecht&count=20
			echo "json succesfull";
			//echo count($json);
		}
		 
		// UNCOMMENT WHEN FINISHED!!
		$DB->tube("update mod_object set llog = now() where ID=$id");
	}
	
	function _content_url($request) {
		return file_get_contents($request);
	}
	
	function _content_curl($request) {
		$ch = curl_init($request);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

		$output = curl_exec($ch);
		curl_close($ch); 
		
		return $output;

	}
	
	function _pull_atom($xml, $feed) {
		// is xml updated since last check?
		$timestamp = strtotime(($xml->entry[0]->updated)? $xml->entry[0]->updated: (($xml->updated)? $xml->updated: $xml->entry[0]->published));
		if($timestamp > strtotime($feed['llog'])) {
			global $DB;
			
			$stored = $DB->parse_by( array("link","date"), "mod_rss", "feed=".$feed['ID']." order by date desc limit 20", true);
			$entries = &$xml->entry;
			foreach($entries as $entry) { 
				foreach($entry->link as $node) if($node->attributes()->rel == "alternate") $link = $node->attributes()->href;
				// is entry already in DB?
				if( ++$n <= 20 && !$stored[(string)$link] ) {
					$string = substr($entry->content, 0, 255);
					$values[] = "(".$DB->_smartq($entry->title).", ".$DB->_smartq($string).", FROM_UNIXTIME(".strtotime($entry->published)."), NOW(), ".$feed['ID'].", '$link')";
				}
			}
			
			// UPDATE DB (if there's any)
			if(count($values)) {
				$DB->tube("insert into mod_rss (`name`, `text`, `date`, `flog`, `feed`, `link`) values ".implode(", ", $values));
				if( ($limit = $DB->short("count(*)","mod_rss","feed = ".$feed['ID'])-20) > 0)
					$DB->tube("delete from mod_rss where feed=".$feed['ID']." order by date limit $limit");
			}
		}
	}
}

?>